package com.aishare.qicaitaoke.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * 作者luwenming
 * 时间2018/1/31
 */

public abstract class BaseFragment extends Fragment{

    boolean isFirst = false;
    View rootView = null;
    boolean isFragmentVisible = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        if(rootView==null){
            rootView = inflater.inflate(getLayoutResources(),container,false);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            isFragmentVisible = true;
        }
        if(rootView == null){
            return;
        }
        //可见，并且没有家在过
        if (!isFirst&&isFragmentVisible){
            onFragmentVisibleChange(true);
            return;
        }
        //由可见--》不可见 已经加载过
        if(isFragmentVisible){
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }

    }

    public void onFragmentVisibleChange(boolean b){

    }

   public abstract int getLayoutResources();
   abstract void initView();
}
